home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / others / ole_101.zip / PATRON.ZIP / OLEVTBL.C < prev    next >
C/C++ Source or Header  |  1992-04-13  |  5KB  |  190 lines

  1. /*
  2.  * OLEVTBL.C
  3.  *
  4.  * Constructors and Destructors for OLECLIENTVTBL and OLESTREAMVTBL
  5.  * structures.  The constructors allocate the structure and initialize
  6.  * the VTBLs within.  The destructors free the VTBL and the structure.
  7.  *
  8.  * Copyright(c) Microsoft Corp. 1992 All Rights Reserved
  9.  */
  10.  
  11.  
  12. #include <windows.h>
  13. #include <ole.h>
  14. #include "oclient.h"
  15.  
  16.  
  17.  
  18. /*
  19.  * PVtblClientAllocate
  20.  *
  21.  * Purpose:
  22.  *  Allocates and initializes an OLECLIENTVTBL structure.
  23.  *
  24.  * Parameters:
  25.  *  pfSuccess       LPBOOL indicating if the initialization succeeded.  If
  26.  *                  this function returns non-NULL, but *pfSuccess==FALSE,
  27.  *                  the caller must call the destructor function.
  28.  *  hInst           HANDLE of the application instance.
  29.  *  pfn             FARPROC to the single client method to initialize.
  30.  *                  We call MakeProcInstance for this function.
  31.  *
  32.  * Return Value:
  33.  *  LPOLECLIENTVTBL Pointer to the allocated VTBL if successful, NULL
  34.  *                  if the allocation failed or a parameter is invalid.
  35.  */
  36.  
  37. LPOLECLIENTVTBL FAR PASCAL PVtblClientAllocate(LPBOOL pfSuccess, HANDLE hInst,
  38.                                                FARPROC pfn)
  39.     {
  40.     LPOLECLIENTVTBL     pvt;
  41.     HANDLE              hMem;
  42.  
  43.     if (NULL==pfSuccess)
  44.         return NULL;
  45.  
  46.     *pfSuccess=FALSE;
  47.  
  48.     if (NULL==hInst || NULL==pfn)
  49.         return NULL;
  50.  
  51.     hMem=LocalAlloc(LPTR, sizeof(OLECLIENTVTBL));
  52.  
  53.     if (NULL==hMem)
  54.         return NULL;
  55.  
  56.     pvt=(LPOLECLIENTVTBL)(PSTR)hMem;
  57.  
  58.     pvt->CallBack=(LPCLIENTCALLBACK)MakeProcInstance(pfn, hInst);
  59.  
  60.     //Indicate success of MakeProcInstance.
  61.     *pfSuccess=(NULL!=pvt->CallBack);
  62.     return pvt;
  63.     }
  64.  
  65.  
  66.  
  67.  
  68. /*
  69.  * PVtblClientFree
  70.  *
  71.  * Purpose:
  72.  *  Frees all procedure instances in the LPOLECLIENTVTBL and frees
  73.  *  the structure as well.
  74.  *
  75.  * Parameters:
  76.  *  pvt             LPOLECLIENTVTBL to the structure to free.
  77.  *
  78.  * Return Value:
  79.  *  LPOLECLIENTVTBL NULL if the function succeeds, pvt otherwise
  80.  */
  81.  
  82. LPOLECLIENTVTBL FAR PASCAL PVtblClientFree(LPOLECLIENTVTBL pvt)
  83.     {
  84.     if (NULL==pvt)
  85.         return pvt;
  86.  
  87.     if (NULL!=pvt->CallBack)
  88.         FreeProcInstance((FARPROC)pvt->CallBack);
  89.  
  90.     /*
  91.      * Since we know in the constructor that this came from local memory,
  92.      * we can assume here that dumping the selector will not hurt us
  93.      * at all, which is a valid assumption (good place for an assert)
  94.      */
  95.     if (NULL!=LocalFree((HANDLE)(DWORD)pvt))
  96.         return pvt;
  97.  
  98.     return NULL;
  99.     }
  100.  
  101.  
  102.  
  103. /*
  104.  * PVtblStreamAllocate
  105.  *
  106.  * Purpose:
  107.  *  Allocates and initializes an OLESTREAMVTBL structure.  It depends
  108.  *  on publics functions named "StreamGet" and "StreamPut" to exist.
  109.  *
  110.  * Parameters:
  111.  *  pfSuccess       LPBOOL indicating if the initialization succeeded.  If
  112.  *                  this function returns non-NULL, but *pfSuccess==FALSE,
  113.  *                  the caller must call the destructor function.
  114.  *  hInst           HANDLE of the application instance.
  115.  *  pfnGet          FARPROC to the stream's Get method.
  116.  *  pfnPut          FARPROC to the stream's Put method.
  117.  *
  118.  * Return Value:
  119.  *  LPOLESTREAMVTBL Pointer to the allocated VTBL if successful, NULL
  120.  *                  if the allocation failed or a parameter is invalid.
  121.  */
  122.  
  123. LPOLESTREAMVTBL FAR PASCAL PVtblStreamAllocate(LPBOOL pfSuccess, HANDLE hInst,
  124.                                                FARPROC pfnGet, FARPROC pfnPut)
  125.     {
  126.     LPOLESTREAMVTBL     pvt;
  127.     HANDLE              hMem;
  128.  
  129.     if (NULL==pfSuccess)
  130.         return NULL;
  131.  
  132.     *pfSuccess=FALSE;
  133.  
  134.     if (NULL==hInst || NULL==pfnGet || NULL==pfnPut)
  135.         return NULL;
  136.  
  137.     hMem=LocalAlloc(LPTR, sizeof(OLESTREAMVTBL));
  138.  
  139.     if (NULL==hMem)
  140.         return NULL;
  141.  
  142.     pvt=(LPOLESTREAMVTBL)(PSTR)hMem;
  143.  
  144.     pvt->Get=(LPSTREAMMETHOD)MakeProcInstance(pfnGet, hInst);
  145.     pvt->Put=(LPSTREAMMETHOD)MakeProcInstance(pfnPut, hInst);
  146.  
  147.     *pfSuccess=(NULL!=pvt->Get) & (NULL!=pvt->Put);
  148.     return pvt;
  149.     }
  150.  
  151.  
  152.  
  153.  
  154. /*
  155.  * PVtblStreamFree
  156.  *
  157.  * Purpose:
  158.  *  Frees all procedure instances in the OLESTREAMVTBL and frees the
  159.  *  structure.
  160.  *
  161.  * Parameters:
  162.  *  pvt             LPOLESTREAMVTBL to the structure to free.
  163.  *
  164.  * Return Value:
  165.  *  LPOLESTREAMVTBL NULL if the function succeeds, pvt otherwise
  166.  *
  167.  */
  168.  
  169. LPOLESTREAMVTBL FAR PASCAL PVtblStreamFree(LPOLESTREAMVTBL pvt)
  170.     {
  171.     if (NULL==pvt)
  172.         return NULL;
  173.  
  174.     if (NULL!=pvt->Get)
  175.         FreeProcInstance((FARPROC)pvt->Get);
  176.  
  177.     if (NULL!=pvt->Put)
  178.         FreeProcInstance((FARPROC)pvt->Put);
  179.  
  180.     /*
  181.      * Since we know in the constructor that this came from local memory,
  182.      * we can assume here that dumping the selector will not hurt us
  183.      * at all, which is a valid assumption (good place for an assert)
  184.      */
  185.     if (NULL!=LocalFree((HANDLE)(DWORD)pvt))
  186.         return pvt;
  187.  
  188.     return NULL;
  189.     }
  190.